//
//  BookingDetailsViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/8/4.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "BookingDetailsViewController.h"

@interface BookingDetailsViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>

@property (nonatomic, strong) UITableView * bookingDetailsTableView;

@property (nonatomic, strong) NSArray * liaoyangTextArray;
@property (nonatomic, strong) NSArray * liaoyangDetailTextArray;

@property (nonatomic, strong) NSArray * tuangouTextArray;
@property (nonatomic, strong) NSArray * tuangouDetailTextArray;

@property (nonatomic, strong) NSArray * qinrenTextArray;
@property (nonatomic, strong) NSArray * qinrenDetailTextArray;

@property (nonatomic, strong) NSArray * houniaoTextArray;
@property (nonatomic, strong) NSArray * houniaoDetailTextArray;

@property (nonatomic, strong) NSString * status;

@end

@implementation BookingDetailsViewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _liaoyangTextArray          = [NSArray array];
        _liaoyangDetailTextArray    = [NSArray array];
        _tuangouTextArray           = [NSArray array];
        _tuangouDetailTextArray     = [NSArray array];
        _qinrenTextArray            = [NSArray array];
        _qinrenDetailTextArray      = [NSArray array];
        _houniaoTextArray           = [NSArray array];
        _houniaoDetailTextArray     = [NSArray array];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self creatData];
    [self creatUI];
    
}

- (void)creatData
{
    NSString * type;
    
    if ([_model.type isEqualToString:@"1"]) {
        type = @"疗养预约";
    } else if ([_model.type isEqualToString:@"2"]) {
        type = @"团购预约";
    } else if ([_model.type isEqualToString:@"3"]) {
        type = @"亲仁节预约";
    } else if ([_model.type isEqualToString:@"4"]) {
        type = @"候鸟养生";
    }

//    NSData *jsonData = [_model.attach dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic;
    dic = [JSONKit jsonToArrayOrDictionary:_model.attach];
    NSString *cardno    = dic[@"cardno"];
    NSString *name      = dic[@"name"];
    NSString *sex       = dic[@"sex"];
    NSString *relation  = dic[@"relation"];
    
    _liaoyangTextArray = @[@"订单号",@"订单状态",@"预约时间",@"姓名",@"会员卡号",@"手机号",@"电话",@"预约类型",@"疗养种类",@"随行人员卡号",@"随行人员姓名",@"随行人员性别",@"与会员关系"];
    _liaoyangDetailTextArray = @[_model.order_no.length>0?_model.order_no:@"暂无信息",
                                 @"未处理订单",
                                 _model.bespeak_time.length>0?_model.bespeak_time:@"暂无信息",
                                 _model.customer_truename.length>0?_model.customer_truename:@"暂无信息",
                                 _model.card_id.length>0?_model.card_id:@"暂无信息",
                                 _model.customer_phone.length>0?_model.customer_phone:@"暂无信息",
                                 _model.customer_tel.length>0?_model.customer_tel:@"暂无信息",
                                 type,
                                 _model.resttype_name.length>0?_model.resttype_name:@"暂无信息",
                                 cardno.length>0?cardno:@"暂无信息",
                                 name.length>0?name:@"暂无信息",
                                 sex.length>0?sex:@"暂无信息",
                                 relation.length>0?relation:@"暂无信息"
                             ];

    _tuangouTextArray = @[@"订单号",@"订单状态",@"预约时间",@"姓名",@"会员卡号",@"手机号",@"电话",@"预约类型",@"商品名称",@"团购数量"];
    _tuangouDetailTextArray = @[_model.order_no.length>0?_model.order_no:@"暂无信息",
                                @"未处理订单",
                                _model.bespeak_time.length>0?_model.bespeak_time:@"暂无信息",
                                _model.customer_truename.length>0?_model.customer_truename:@"暂无信息",
                                _model.card_id.length>0?_model.card_id:@"暂无信息",
                                _model.customer_phone.length>0?_model.customer_phone:@"暂无信息",
                                _model.customer_tel.length>0?_model.customer_tel:@"暂无信息",
                                type,
                                _model.goods_name.length>0?_model.goods_name:@"暂无信息",
                                _model.goods_count.length>0?_model.goods_count:@"暂无信息"
                                ];
    
    
    _qinrenTextArray = @[@"订单号",@"订单状态",@"预约时间",@"姓名",@"会员卡号",@"手机号",@"电话",@"预约类型",@"下单时间",@"亲仁节名称",@"随行人员卡号",@"随行人员姓名",@"随行人员性别",@"与会员关系"];
    _qinrenDetailTextArray = @[_model.order_no.length>0?_model.order_no:@"暂无信息",
                               @"未处理订单",
                               _model.bespeak_time.length>0?_model.bespeak_time:@"暂无信息",
                               _model.customer_truename.length>0?_model.customer_truename:@"暂无信息",
                               _model.card_id.length>0?_model.card_id:@"暂无信息",
                               _model.customer_phone.length>0?_model.customer_phone:@"暂无信息",
                               _model.customer_tel.length>0?_model.customer_tel:@"暂无信息",
                               type,
                               _model.create_time.length>0?_model.create_time:@"暂无信息",
                               _model.qinrenday_name.length>0?_model.qinrenday_name:@"暂无信息",
                               cardno.length>0?cardno:@"暂无信息",
                               name.length>0?name:@"暂无信息",
                               sex.length>0?sex:@"暂无信息",
                               relation.length>0?relation:@"暂无信息"
                               ];
    
    _houniaoTextArray = @[@"订单号",@"订单状态",@"姓名",@"会员卡号",@"手机号",@"电话",@"预约类型",@"预约人数",@"基地名称",@"预约开始时间",@"预约结束时间"];
    _houniaoDetailTextArray = @[_model.order_no.length>0?_model.order_no:@"暂无信息",
                                @"未处理订单",
                                _model.customer_truename.length>0?_model.customer_truename:@"暂无信息",
                                _model.card_id.length>0?_model.card_id:@"暂无信息",
                                _model.customer_phone.length>0?_model.customer_phone:@"暂无信息",
                                _model.customer_tel.length>0?_model.customer_tel:@"暂无信息",
                                type,
                                _model.sf_count.length>0?_model.sf_count:@"暂无信息",
                                _model.sf_name.length>0?_model.sf_name:@"暂无信息",
                                _model.start_time.length>0?_model.start_time:@"暂无信息",
                                _model.end_time.length>0?_model.end_time:@"暂无信息"
                                ];
    
}

- (void)creatUI
{
    if (_type) {
        UIButton *checkButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        [checkButton setTitle:@"审核" forState:UIControlStateNormal];
        [checkButton addTarget:self action:@selector(checkClilc) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *checkItem = [[UIBarButtonItem alloc] initWithCustomView:checkButton];
        [self.navigationItem setRightBarButtonItem:checkItem];    
    }
    
    _bookingDetailsTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    _bookingDetailsTableView.backgroundColor = [UIColor clearColor];
    _bookingDetailsTableView.delegate = self;
    _bookingDetailsTableView.dataSource = self;
    _bookingDetailsTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:_bookingDetailsTableView];

    
}

- (void)upLoadStatus {

    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *parameters = @{@"bespeak_id":self.model.idNum,@"status":_status};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"update.user.bespeak" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"审核的dictonary :%@",[dictionary description]);
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
            [SVProgressHUD showSuccessWithStatus:@"审核成功"];
            
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];



}

- (void)checkClilc {

    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"审核"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"不通过"
                                  otherButtonTitles:@"通过",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) {
        NSLog(@"不通过");
        _status = @"2";
        [self upLoadStatus];
    }
    else if (buttonIndex == 1) {
        NSLog(@"通过");
        _status = @"1";
        [self upLoadStatus];

    }

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger i = [_model.type integerValue];
    if (i == 1) {
        return [self.liaoyangTextArray count];
        
    }else if(i == 2) {
        return [self.tuangouTextArray count];
        
    }else if(i == 3) {
        return [self.qinrenTextArray count];
        
    }else if(i == 4) {
        return [self.houniaoTextArray count];
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"BookingDetailsCell";
    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }

    NSInteger i = [_model.type integerValue];
    if (i == 1) {
        cell.textLabel.text = _liaoyangTextArray[indexPath.row];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
        
        cell.detailTextLabel.text = _liaoyangDetailTextArray[indexPath.row];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }else if(i == 2) {
        cell.textLabel.text = _tuangouTextArray[indexPath.row];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
        
        cell.detailTextLabel.text = _tuangouDetailTextArray[indexPath.row];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;

        
    }else if(i == 3) {
        cell.textLabel.text = _qinrenTextArray[indexPath.row];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
        
        cell.detailTextLabel.text = _qinrenDetailTextArray[indexPath.row];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;

        
    }else if(i == 4) {
        cell.textLabel.text = _houniaoTextArray[indexPath.row];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
        
        cell.detailTextLabel.text = _houniaoDetailTextArray[indexPath.row];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;

    }

    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}




@end
